package Servlet.User;
//登录
import BaseClass.UserInfo;
import DataBase.DataBase;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * 登录成功
 */
public class LoginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取输入用户名
        String username = req.getParameter("username");
        //获取输入密码
        String password  = req.getParameter("password");
        //获取输入验证码
        String VerificationCode = req.getParameter("VerificationCode").toUpperCase();
        //获取实际验证码
        HttpSession HS = req.getSession();
        String trueVerificationCode = HS.getAttribute("VCode").toString().toUpperCase();

        resp.setCharacterEncoding("UTF-8");

        if(VerificationCode.equals(trueVerificationCode)){
                if(DataBase.JudgeByUsernameAndPassword(username,password)){//密码正确
                    UserInfo userInfo = DataBase.GetUserInfoByUsername(username);

                    HS.setAttribute("username",username);

                    HS.setAttribute("name",userInfo.getName());
                    HS.setAttribute("location",userInfo.getLocation());

                    //存储日志
                    String Servlet = req.getServletPath();
                    DataBase.setLogInfo(username,Servlet);

                    resp.getWriter().print("true");
                }

                else{
                    if(DataBase.HaveRegister(username))
                        resp.getWriter().print("false password");
                    else
                        resp.getWriter().print("false username");
                }

        }
        else{
            resp.getWriter().print("false VerificationCode");
        }

    }
}
